﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class UD61720_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过

        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private byte UD61720_0XFE_XX = 0;
        private static bool Gamma_adjust_timeout_flag = false;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        private int step_l = 1;
        //private int step_m = 5;
        private int step_h = 12;

        //监听UD61720挂载
        frmMain frmHelp = null;
        public UD61720_Gamma(frmMain UD61720)
        {
            frmHelp = UD61720;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体            

        }
        //VT5100 监听卸载
        private void UD61720_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(UD61720_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "UD61720_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void UD61720_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(UD61720_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "UD61720_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(UD61720_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "UD61720_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }



        //串口接收
        private void UD61720_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x4C: UD61720的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x4C && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x4C)      //UD61720
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        if (Setting.TempRecord.UD61720CmdType == 0x02)  //register read
                                        {
                                            string tmp = "";
                                            for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                            {
                                                tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                            }
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                            UD61720_txtRegData.Text = tmp;
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xFF) == 0x00)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xFF) == 0x42)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xFF) == 0x81)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x11:
                                    {
                                        switch (Rec_tmp[3])
                                        {
                                            case 0x01:
                                            case 0x02:
                                            case 0x03:
                                                {
                                                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma切换成功！\r\n";
                                                }
                                                break;
                                            default: break;
                                        }
                                        /*if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma切换成功！\r\n";
                                        }*/
                                    }
                                    break;

                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Gamma\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Gamma\t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP GOA\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP GOA\t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP POWER\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP POWER\t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP OTHERS\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP OTHERS\t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        textBox8.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Time\t读 OK！\r\n";
                                    }
                                    break;

                                case 0x09:
                                    {
                                        if (Setting.TempRecord.UD61720CmdType == 0x03)
                                        {

                                            UD61720_R0.Value = Decimal.Parse(Rec_tmp[4].ToString());
                                            UD61720_R4.Value = Decimal.Parse(Rec_tmp[5].ToString()) * 4 + Decimal.Parse((Rec_tmp[21] & 0x03).ToString());
                                            UD61720_R8.Value = Decimal.Parse(Rec_tmp[6].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x0C) >> 2)).ToString());
                                            UD61720_R12.Value = Decimal.Parse(Rec_tmp[7].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x30) >> 4)).ToString());
                                            UD61720_R16.Value = Decimal.Parse(Rec_tmp[8].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0xC0) >> 6)).ToString());


                                            UD61720_R32.Value = Decimal.Parse(Rec_tmp[9].ToString()) * 4 + Decimal.Parse((Rec_tmp[22] & 0x03).ToString());
                                            UD61720_R64.Value = Decimal.Parse(Rec_tmp[10].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x0C) >> 2)).ToString());
                                            UD61720_R96.Value = Decimal.Parse(Rec_tmp[11].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x30) >> 4)).ToString());
                                            UD61720_R128.Value = Decimal.Parse(Rec_tmp[12].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0xC0) >> 6)).ToString());

                                            UD61720_R160.Value = Decimal.Parse(Rec_tmp[13].ToString()) * 4 + Decimal.Parse((Rec_tmp[23] & 0x03).ToString());
                                            UD61720_R192.Value = Decimal.Parse(Rec_tmp[14].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x0C) >> 2)).ToString());
                                            UD61720_R224.Value = Decimal.Parse(Rec_tmp[15].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x30) >> 4)).ToString());
                                            UD61720_R240.Value = Decimal.Parse(Rec_tmp[16].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0xC0) >> 6)).ToString());

                                            UD61720_R244.Value = Decimal.Parse(Rec_tmp[17].ToString()) * 4 + Decimal.Parse((Rec_tmp[24] & 0x03).ToString());
                                            UD61720_R248.Value = Decimal.Parse(Rec_tmp[18].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x0C) >> 2)).ToString());
                                            UD61720_R252.Value = Decimal.Parse(Rec_tmp[19].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x30) >> 4)).ToString());
                                            UD61720_R255.Value = Decimal.Parse(Rec_tmp[20].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0xC0) >> 6)).ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }

                                        else if (Setting.TempRecord.UD61720CmdType == 0x04)
                                        {

                                            UD61720_G0.Value = Decimal.Parse(Rec_tmp[4].ToString());
                                            UD61720_G4.Value = Decimal.Parse(Rec_tmp[5].ToString()) * 4 + Decimal.Parse((Rec_tmp[21] & 0x03).ToString());
                                            UD61720_G8.Value = Decimal.Parse(Rec_tmp[6].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x0C) >> 2)).ToString());
                                            UD61720_G12.Value = Decimal.Parse(Rec_tmp[7].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x30) >> 4)).ToString());
                                            UD61720_G16.Value = Decimal.Parse(Rec_tmp[8].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0xC0) >> 6)).ToString());


                                            UD61720_G32.Value = Decimal.Parse(Rec_tmp[9].ToString()) * 4 + Decimal.Parse((Rec_tmp[22] & 0x03).ToString());
                                            UD61720_G64.Value = Decimal.Parse(Rec_tmp[10].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x0C) >> 2)).ToString());
                                            UD61720_G96.Value = Decimal.Parse(Rec_tmp[11].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x30) >> 4)).ToString());
                                            UD61720_G128.Value = Decimal.Parse(Rec_tmp[12].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0xC0) >> 6)).ToString());

                                            UD61720_G160.Value = Decimal.Parse(Rec_tmp[13].ToString()) * 4 + Decimal.Parse((Rec_tmp[23] & 0x03).ToString());
                                            UD61720_G192.Value = Decimal.Parse(Rec_tmp[14].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x0C) >> 2)).ToString());
                                            UD61720_G224.Value = Decimal.Parse(Rec_tmp[15].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x30) >> 4)).ToString());
                                            UD61720_G240.Value = Decimal.Parse(Rec_tmp[16].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0xC0) >> 6)).ToString());

                                            UD61720_G244.Value = Decimal.Parse(Rec_tmp[17].ToString()) * 4 + Decimal.Parse((Rec_tmp[24] & 0x03).ToString());
                                            UD61720_G248.Value = Decimal.Parse(Rec_tmp[18].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x0C) >> 2)).ToString());
                                            UD61720_G252.Value = Decimal.Parse(Rec_tmp[19].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x30) >> 4)).ToString());
                                            UD61720_G255.Value = Decimal.Parse(Rec_tmp[20].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0xC0) >> 6)).ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }

                                        else if (Setting.TempRecord.UD61720CmdType == 0x05)
                                        {

                                            UD61720_B0.Value = Decimal.Parse(Rec_tmp[4].ToString());
                                            UD61720_B4.Value = Decimal.Parse(Rec_tmp[5].ToString()) * 4 + Decimal.Parse((Rec_tmp[21] & 0x03).ToString());
                                            UD61720_B8.Value = Decimal.Parse(Rec_tmp[6].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x0C) >> 2)).ToString());
                                            UD61720_B12.Value = Decimal.Parse(Rec_tmp[7].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0x30) >> 4)).ToString());
                                            UD61720_B16.Value = Decimal.Parse(Rec_tmp[8].ToString()) * 4 + Decimal.Parse((((Rec_tmp[21] & 0xC0) >> 6)).ToString());


                                            UD61720_B32.Value = Decimal.Parse(Rec_tmp[9].ToString()) * 4 + Decimal.Parse((Rec_tmp[22] & 0x03).ToString());
                                            UD61720_B64.Value = Decimal.Parse(Rec_tmp[10].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x0C) >> 2)).ToString());
                                            UD61720_B96.Value = Decimal.Parse(Rec_tmp[11].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0x30) >> 4)).ToString());
                                            UD61720_B128.Value = Decimal.Parse(Rec_tmp[12].ToString()) * 4 + Decimal.Parse((((Rec_tmp[22] & 0xC0) >> 6)).ToString());

                                            UD61720_B160.Value = Decimal.Parse(Rec_tmp[13].ToString()) * 4 + Decimal.Parse((Rec_tmp[23] & 0x03).ToString());
                                            UD61720_B192.Value = Decimal.Parse(Rec_tmp[14].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x0C) >> 2)).ToString());
                                            UD61720_B224.Value = Decimal.Parse(Rec_tmp[15].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0x30) >> 4)).ToString());
                                            UD61720_B240.Value = Decimal.Parse(Rec_tmp[16].ToString()) * 4 + Decimal.Parse((((Rec_tmp[23] & 0xC0) >> 6)).ToString());

                                            UD61720_B244.Value = Decimal.Parse(Rec_tmp[17].ToString()) * 4 + Decimal.Parse((Rec_tmp[24] & 0x03).ToString());
                                            UD61720_B248.Value = Decimal.Parse(Rec_tmp[18].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x0C) >> 2)).ToString());
                                            UD61720_B252.Value = Decimal.Parse(Rec_tmp[19].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0x30) >> 4)).ToString());
                                            UD61720_B255.Value = Decimal.Parse(Rec_tmp[20].ToString()) * 4 + Decimal.Parse((((Rec_tmp[24] & 0xC0) >> 6)).ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;
                                default: break;

                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void UD61720_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + UD61720_R0.Text + "," + "G0," + UD61720_G0.Text + "," + "B0," + UD61720_B0.Text + "\r\n";
                    str = str + "R4," + UD61720_R4.Text + "," + "G4," + UD61720_G4.Text + "," + "B4," + UD61720_B4.Text + "\r\n";
                    str = str + "R8," + UD61720_R8.Text + "," + "G8," + UD61720_G8.Text + "," + "B8," + UD61720_B8.Text + "\r\n";
                    str = str + "R12," + UD61720_R12.Text + "," + "G12," + UD61720_G12.Text + "," + "B12," + UD61720_B12.Text + "\r\n";
                    str = str + "R16," + UD61720_R16.Text + "," + "G16," + UD61720_G16.Text + "," + "B16," + UD61720_B16.Text + "\r\n";
                    str = str + "R32," + UD61720_R32.Text + "," + "G32," + UD61720_G32.Text + "," + "B32," + UD61720_B32.Text + "\r\n";
                    str = str + "R64," + UD61720_R64.Text + "," + "G64," + UD61720_G64.Text + "," + "B64," + UD61720_B64.Text + "\r\n";
                    str = str + "R96," + UD61720_R96.Text + "," + "G96," + UD61720_G96.Text + "," + "B96," + UD61720_B96.Text + "\r\n";
                    str = str + "R128," + UD61720_R128.Text + "," + "G128," + UD61720_G128.Text + "," + "B128," + UD61720_B128.Text + "\r\n";
                    str = str + "R160," + UD61720_R160.Text + "," + "G160," + UD61720_G160.Text + "," + "B160," + UD61720_B160.Text + "\r\n";
                    str = str + "R192," + UD61720_R192.Text + "," + "G192," + UD61720_G192.Text + "," + "B192," + UD61720_B192.Text + "\r\n";
                    str = str + "R224," + UD61720_R224.Text + "," + "G224," + UD61720_G224.Text + "," + "B224," + UD61720_B224.Text + "\r\n";
                    str = str + "R240," + UD61720_R240.Text + "," + "G240," + UD61720_G240.Text + "," + "B240," + UD61720_B240.Text + "\r\n";
                    str = str + "R244," + UD61720_R244.Text + "," + "G244," + UD61720_G244.Text + "," + "B244," + UD61720_B244.Text + "\r\n";
                    str = str + "R248," + UD61720_R248.Text + "," + "G248," + UD61720_G248.Text + "," + "B248," + UD61720_B248.Text + "\r\n";
                    str = str + "R252," + UD61720_R252.Text + "," + "G252," + UD61720_G252.Text + "," + "B252," + UD61720_B252.Text + "\r\n";
                    str = str + "R255," + UD61720_R255.Text + "," + "G255," + UD61720_G255.Text + "," + "B255," + UD61720_B255.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void UD61720_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                UD61720_R0.Text = arrtmp[1].ToString(); UD61720_G0.Text = arrtmp[3].ToString(); UD61720_B0.Text = arrtmp[5].ToString();
                UD61720_R4.Text = arrtmp[7].ToString(); UD61720_G4.Text = arrtmp[9].ToString(); UD61720_B4.Text = arrtmp[11].ToString();
                UD61720_R8.Text = arrtmp[13].ToString(); UD61720_G8.Text = arrtmp[15].ToString(); UD61720_B8.Text = arrtmp[17].ToString();
                UD61720_R12.Text = arrtmp[19].ToString(); UD61720_G12.Text = arrtmp[21].ToString(); UD61720_B12.Text = arrtmp[23].ToString();
                UD61720_R16.Text = arrtmp[25].ToString(); UD61720_G16.Text = arrtmp[27].ToString(); UD61720_B16.Text = arrtmp[29].ToString();
                UD61720_R32.Text = arrtmp[31].ToString(); UD61720_G32.Text = arrtmp[33].ToString(); UD61720_B32.Text = arrtmp[35].ToString();
                UD61720_R64.Text = arrtmp[37].ToString(); UD61720_G64.Text = arrtmp[39].ToString(); UD61720_B64.Text = arrtmp[41].ToString();
                UD61720_R96.Text = arrtmp[43].ToString(); UD61720_G96.Text = arrtmp[45].ToString(); UD61720_B96.Text = arrtmp[47].ToString();
                UD61720_R128.Text = arrtmp[49].ToString(); UD61720_G128.Text = arrtmp[51].ToString(); UD61720_B128.Text = arrtmp[53].ToString();
                UD61720_R160.Text = arrtmp[55].ToString(); UD61720_G160.Text = arrtmp[57].ToString(); UD61720_B160.Text = arrtmp[59].ToString();
                UD61720_R192.Text = arrtmp[61].ToString(); UD61720_G192.Text = arrtmp[63].ToString(); UD61720_B192.Text = arrtmp[65].ToString();
                UD61720_R224.Text = arrtmp[67].ToString(); UD61720_G224.Text = arrtmp[69].ToString(); UD61720_B224.Text = arrtmp[71].ToString();
                UD61720_R240.Text = arrtmp[73].ToString(); UD61720_G240.Text = arrtmp[75].ToString(); UD61720_B240.Text = arrtmp[77].ToString();
                UD61720_R244.Text = arrtmp[79].ToString(); UD61720_G244.Text = arrtmp[81].ToString(); UD61720_B244.Text = arrtmp[83].ToString();
                UD61720_R248.Text = arrtmp[85].ToString(); UD61720_G248.Text = arrtmp[87].ToString(); UD61720_B248.Text = arrtmp[89].ToString();
                UD61720_R252.Text = arrtmp[91].ToString(); UD61720_G252.Text = arrtmp[93].ToString(); UD61720_B252.Text = arrtmp[95].ToString();
                UD61720_R255.Text = arrtmp[97].ToString(); UD61720_G255.Text = arrtmp[99].ToString(); UD61720_B255.Text = arrtmp[101].ToString();
            }
        }




        //------------------------------------------UD61720_START------------------------------------------------
        private void UD61720_GAMMA_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[27];
            Setting.TempRecord.cmdLen = 27;
            Send_buf[2] = 22;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4C; //UD61720


            switch (btnTmp.Name)
            {
                case "UD61720_GammaR_Red":
                    {

                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        Setting.TempRecord.UD61720CmdType = 0x03;       //      3
                    }
                    break;

                case "UD61720_GammaR_Green":
                    {

                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x04;                        //绿色 green
                        Setting.TempRecord.UD61720CmdType = 0x04;   //      4

                    }
                    break;
                case "UD61720_GammaR_Blue": //              9
                    {

                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x08;                        //   blue  
                        Setting.TempRecord.UD61720CmdType = 0x05;   //      5

                    }
                    break;
                //--------------------------------------
                case "UD61720_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色1

                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(UD61720_R0.Value.ToString()));
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(UD61720_R4.Value.ToString()) >> 2) & 0xff);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(UD61720_R8.Value.ToString()) >> 2) & 0xff);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(UD61720_R12.Value.ToString()) >> 2) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(UD61720_R16.Value.ToString()) >> 2) & 0xff);

                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(UD61720_R32.Value.ToString()) >> 2) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(UD61720_R64.Value.ToString()) >> 2) & 0xff);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(UD61720_R96.Value.ToString()) >> 2) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(UD61720_R128.Value.ToString()) >> 2) & 0xff);

                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(UD61720_R160.Value.ToString()) >> 2) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(UD61720_R192.Value.ToString()) >> 2) & 0xff);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(UD61720_R224.Value.ToString()) >> 2) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(UD61720_R240.Value.ToString()) >> 2) & 0xff);

                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(UD61720_R244.Value.ToString()) >> 2) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(UD61720_R248.Value.ToString()) >> 2) & 0xff);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(UD61720_R252.Value.ToString()) >> 2) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(UD61720_R255.Value.ToString()) >> 2) & 0xff);

                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(UD61720_R4.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_R8.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_R12.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_R16.Value.ToString()) & 0x03) << 6));
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(UD61720_R32.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_R64.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_R96.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_R128.Value.ToString()) & 0x03) << 6));
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(UD61720_R160.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_R192.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_R224.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_R240.Value.ToString()) & 0x03) << 6));
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(UD61720_R244.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_R248.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_R252.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_R255.Value.ToString()) & 0x03) << 6));

                    }
                    break;

                case "UD61720_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色1

                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(UD61720_G0.Value.ToString()));
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(UD61720_G4.Value.ToString()) >> 2) & 0xff);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(UD61720_G8.Value.ToString()) >> 2) & 0xff);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(UD61720_G12.Value.ToString()) >> 2) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(UD61720_G16.Value.ToString()) >> 2) & 0xff);

                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(UD61720_G32.Value.ToString()) >> 2) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(UD61720_G64.Value.ToString()) >> 2) & 0xff);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(UD61720_G96.Value.ToString()) >> 2) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(UD61720_G128.Value.ToString()) >> 2) & 0xff);

                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(UD61720_G160.Value.ToString()) >> 2) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(UD61720_G192.Value.ToString()) >> 2) & 0xff);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(UD61720_G224.Value.ToString()) >> 2) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(UD61720_G240.Value.ToString()) >> 2) & 0xff);

                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(UD61720_G244.Value.ToString()) >> 2) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(UD61720_G248.Value.ToString()) >> 2) & 0xff);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(UD61720_G252.Value.ToString()) >> 2) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(UD61720_G255.Value.ToString()) >> 2) & 0xff);

                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(UD61720_G4.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_G8.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_G12.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_G16.Value.ToString()) & 0x03) << 6));
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(UD61720_G32.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_G64.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_G96.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_G128.Value.ToString()) & 0x03) << 6));
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(UD61720_G160.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_G192.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_G224.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_G240.Value.ToString()) & 0x03) << 6));
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(UD61720_G244.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_G248.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_G252.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_G255.Value.ToString()) & 0x03) << 6));

                    }
                    break;

                case "UD61720_GammaW_Blue":     //      8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;

                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(UD61720_B0.Value.ToString()));
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(UD61720_B4.Value.ToString()) >> 2) & 0xff);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(UD61720_B8.Value.ToString()) >> 2) & 0xff);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(UD61720_B12.Value.ToString()) >> 2) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(UD61720_B16.Value.ToString()) >> 2) & 0xff);

                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(UD61720_B32.Value.ToString()) >> 2) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(UD61720_B64.Value.ToString()) >> 2) & 0xff);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(UD61720_B96.Value.ToString()) >> 2) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(UD61720_B128.Value.ToString()) >> 2) & 0xff);

                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(UD61720_B160.Value.ToString()) >> 2) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(UD61720_B192.Value.ToString()) >> 2) & 0xff);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(UD61720_B224.Value.ToString()) >> 2) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(UD61720_B240.Value.ToString()) >> 2) & 0xff);

                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(UD61720_B244.Value.ToString()) >> 2) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(UD61720_B248.Value.ToString()) >> 2) & 0xff);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(UD61720_B252.Value.ToString()) >> 2) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(UD61720_B255.Value.ToString()) >> 2) & 0xff);

                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(UD61720_B4.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_B8.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_B12.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_B16.Value.ToString()) & 0x03) << 6));
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(UD61720_B32.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_B64.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_B96.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_B128.Value.ToString()) & 0x03) << 6));
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(UD61720_B160.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_B192.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_B224.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_B240.Value.ToString()) & 0x03) << 6));
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(UD61720_B244.Value.ToString()) & 0x03) | ((Convert.ToUInt16(UD61720_B248.Value.ToString()) & 0x03) << 2) | ((Convert.ToUInt16(UD61720_B252.Value.ToString()) & 0x03) << 4) | ((Convert.ToUInt16(UD61720_B255.Value.ToString()) & 0x03) << 6));

                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[25], out Send_buf[26]);
            try
            {
                UD61720_0XFE_XX = 0x00;
                frmHelp.comPort.Write(Send_buf, 0, 27);   //UD61720
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void UD61720_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(UD61720_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "UD61720_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x4C;//UD61720

                        if (UD61720_rbt_LP.Checked == true)
                        { Send_buf[1] = 0x01; }

                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "UD61720_51Reg_W":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x4C;
                        Send_buf[1] = 0x03;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "UD61720_RegR":
                    {
                        Setting.TempRecord.UD61720CmdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(UD61720_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x4C; //UD61720
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());  //cmd
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                default: break;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void UD61720_trackBar_Scroll(object sender, EventArgs e)
        {
            UD61720_label.Text = UD61720_51Reg_trackBar.Value.ToString() + "阶";
            UD61720_txtRegData.Text = "51," + UD61720_51Reg_trackBar.Value.ToString("x2");
            UD61720_RegWR_Click(UD61720_51Reg_W, e);
        }
        private void UD61720_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x4C;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "UD61720_OTP_Gamma": //otp Gamma       A
                    {
                        UD61720_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0A;
                    }
                    break;
                case "UD61720_OTP_GOA"://otp GOA        D
                    {
                        UD61720_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                    }
                    break;
                case "UD61720_OTP_Power"://otp POWER        E
                    {
                        UD61720_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0E;
                    }
                    break;
                case "UD61720_OTP_Others"://otp OTHERS                    F
                    {
                        UD61720_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0F;
                    }
                    break;
                case "UD61720_Read_Time":       //          B
                    {
                        UD61720_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0B;
                        Setting.TempRecord.UD61720CmdType = 0x02;       //      2
                        if (radioButton18.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (radioButton10.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (radioButton9.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        else if (radioButton6.Checked == true)
                        {
                            Send_buf[3] = 0x03;
                        }
                    }
                    break;
                case "UD61720_Wr_Time":         //w写寄存器   C
                    {
                        UD61720_0XFE_XX = 0x00;
                        if (Convert.ToDecimal(textBox8.Text) > 3)
                        {
                            MessageBox.Show("OTP次数不能大于3次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(textBox8.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void UD61720_auto_gamma(Object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 16)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "UD61720 gamma调试完毕!\r\n";
                }
                if (index < 16)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(textUD_252_off.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(textUD_248_off.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(textUD_244_off.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(textUD_240_off.Text) * Convert.ToDouble(txtUD_240.Text) / 100;
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(textUD_224_off.Text) * Convert.ToDouble(txtUD_224.Text) / 100;
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(textUD_192_off.Text) * Convert.ToDouble(txtUD_192.Text) / 100;
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(textUD_160_off.Text) * Convert.ToDouble(txtUD_160.Text) / 100;
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(textUD_128_off.Text) * Convert.ToDouble(txtUD_128.Text) / 100;
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(textUD_96_off.Text) * Convert.ToDouble(txtUD_96.Text) / 100;
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(textUD_64_off.Text) * Convert.ToDouble(txtUD_64.Text) / 100;
                            break;
                        case 11:
                            {
                                lum_off = Convert.ToDouble(textUD_32_off.Text) * Convert.ToDouble(txtUD_32.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 12:
                            {
                                lum_off = Convert.ToDouble(textUD_16_off.Text) * Convert.ToDouble(txtUD_16.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 13:
                            {
                                lum_off = Convert.ToDouble(textUD_12_off.Text) * Convert.ToDouble(txtUD_12.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 14:
                            {
                                lum_off = Convert.ToDouble(textUD_8_off.Text) * Convert.ToDouble(txtUD_8.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 15:
                            {
                                lum_off = Convert.ToDouble(textUD_4_off.Text) * Convert.ToDouble(txtUD_4.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                    }
                    frmHelp.numPatternR.Value = Parttern_UD61720[index];
                    frmHelp.numPatternG.Value = Parttern_UD61720[index];
                    frmHelp.numPatternB.Value = Parttern_UD61720[index];
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                Lr_t = CaSDKObj.R_y * ((frmHelp.CASDK_y - CaSDKObj.G_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.G_y) * (frmHelp.CASDK_x - CaSDKObj.R_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (CaSDKObj.R_y - frmHelp.CASDK_y));
                Lb_t = 1.0;
                Lg_t = CaSDKObj.G_y * ((frmHelp.CASDK_y - CaSDKObj.R_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.R_y) * (frmHelp.CASDK_x - CaSDKObj.G_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (CaSDKObj.G_y - frmHelp.CASDK_y));
                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);

                switch (index)
                {
                    case 0:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R255.Value = UD61720_R255.Value - step_h;
                                    else UD61720_R255.Value = UD61720_R255.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R255.Value = UD61720_R255.Value - step_l;
                                    else UD61720_R255.Value = UD61720_R255.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G255.Value = UD61720_G255.Value - step_h;
                                    else UD61720_G255.Value = UD61720_G255.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G255.Value = UD61720_G255.Value - step_l;
                                    else UD61720_G255.Value = UD61720_G255.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B255.Value = UD61720_B255.Value - step_h;
                                    else UD61720_B255.Value = UD61720_B255.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B255.Value = UD61720_B255.Value - step_l;
                                    else UD61720_B255.Value = UD61720_B255.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 1:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R252.Value = UD61720_R252.Value - step_h;
                                    else UD61720_R252.Value = UD61720_R252.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R252.Value = UD61720_R252.Value - step_l;
                                    else UD61720_R252.Value = UD61720_R252.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G252.Value = UD61720_G252.Value - step_h;
                                    else UD61720_G252.Value = UD61720_G252.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G252.Value = UD61720_G252.Value - step_l;
                                    else UD61720_G252.Value = UD61720_G252.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B252.Value = UD61720_B252.Value - step_h;
                                    else UD61720_B252.Value = UD61720_B252.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B252.Value = UD61720_B252.Value - step_l;
                                    else UD61720_B252.Value = UD61720_B252.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 2:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R248.Value = UD61720_R248.Value - step_h;
                                    else UD61720_R248.Value = UD61720_R248.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R248.Value = UD61720_R248.Value - step_l;
                                    else UD61720_R248.Value = UD61720_R248.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G248.Value = UD61720_G248.Value - step_h;
                                    else UD61720_G248.Value = UD61720_G248.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G248.Value = UD61720_G248.Value - step_l;
                                    else UD61720_G248.Value = UD61720_G248.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B248.Value = UD61720_B248.Value - step_h;
                                    else UD61720_B248.Value = UD61720_B248.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B248.Value = UD61720_B248.Value - step_l;
                                    else UD61720_B248.Value = UD61720_B248.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 3:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R244.Value = UD61720_R244.Value - step_h;
                                    else UD61720_R244.Value = UD61720_R244.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R244.Value = UD61720_R244.Value - step_l;
                                    else UD61720_R244.Value = UD61720_R244.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G244.Value = UD61720_G244.Value - step_h;
                                    else UD61720_G244.Value = UD61720_G244.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G244.Value = UD61720_G244.Value - step_l;
                                    else UD61720_G244.Value = UD61720_G244.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B244.Value = UD61720_B244.Value - step_h;
                                    else UD61720_B244.Value = UD61720_B244.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B244.Value = UD61720_B244.Value - step_l;
                                    else UD61720_B244.Value = UD61720_B244.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 4:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R240.Value = UD61720_R240.Value - step_h;
                                    else UD61720_R240.Value = UD61720_R240.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R240.Value = UD61720_R240.Value - step_l;
                                    else UD61720_R240.Value = UD61720_R240.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G240.Value = UD61720_G240.Value - step_h;
                                    else UD61720_G240.Value = UD61720_G240.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G240.Value = UD61720_G240.Value - step_l;
                                    else UD61720_G240.Value = UD61720_G240.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B240.Value = UD61720_B240.Value - step_h;
                                    else UD61720_B240.Value = UD61720_B240.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B240.Value = UD61720_B240.Value - step_l;
                                    else UD61720_B240.Value = UD61720_B240.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 5:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R224.Value = UD61720_R224.Value - step_h;
                                    else UD61720_R224.Value = UD61720_R224.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R224.Value = UD61720_R224.Value - step_l;
                                    else UD61720_R224.Value = UD61720_R224.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G224.Value = UD61720_G224.Value - step_h;
                                    else UD61720_G224.Value = UD61720_G224.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G224.Value = UD61720_G224.Value - step_l;
                                    else UD61720_G224.Value = UD61720_G224.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B224.Value = UD61720_B224.Value - step_h;
                                    else UD61720_B224.Value = UD61720_B224.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B224.Value = UD61720_B224.Value - step_l;
                                    else UD61720_B224.Value = UD61720_B224.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 6:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R192.Value = UD61720_R192.Value - step_h;
                                    else UD61720_R192.Value = UD61720_R192.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R192.Value = UD61720_R192.Value - step_l;
                                    else UD61720_R192.Value = UD61720_R192.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G192.Value = UD61720_G192.Value - step_h;
                                    else UD61720_G192.Value = UD61720_G192.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G192.Value = UD61720_G192.Value - step_l;
                                    else UD61720_G192.Value = UD61720_G192.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B192.Value = UD61720_B192.Value - step_h;
                                    else UD61720_B192.Value = UD61720_B192.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B192.Value = UD61720_B192.Value - step_l;
                                    else UD61720_B192.Value = UD61720_B192.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 7:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R160.Value = UD61720_R160.Value - step_h;
                                    else UD61720_R160.Value = UD61720_R160.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R160.Value = UD61720_R160.Value - step_l;
                                    else UD61720_R160.Value = UD61720_R160.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G160.Value = UD61720_G160.Value - step_h;
                                    else UD61720_G160.Value = UD61720_G160.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G160.Value = UD61720_G160.Value - step_l;
                                    else UD61720_G160.Value = UD61720_G160.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B160.Value = UD61720_B160.Value - step_h;
                                    else UD61720_B160.Value = UD61720_B160.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B160.Value = UD61720_B160.Value - step_l;
                                    else UD61720_B160.Value = UD61720_B160.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 8:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R128.Value = UD61720_R128.Value - step_h;
                                    else UD61720_R128.Value = UD61720_R128.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R128.Value = UD61720_R128.Value - step_l;
                                    else UD61720_R128.Value = UD61720_R128.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G128.Value = UD61720_G128.Value - step_h;
                                    else UD61720_G128.Value = UD61720_G128.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G128.Value = UD61720_G128.Value - step_l;
                                    else UD61720_G128.Value = UD61720_G128.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B128.Value = UD61720_B128.Value - step_h;
                                    else UD61720_B128.Value = UD61720_B128.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B128.Value = UD61720_B128.Value - step_l;
                                    else UD61720_B128.Value = UD61720_B128.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 9:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R96.Value = UD61720_R96.Value - step_h;
                                    else UD61720_R96.Value = UD61720_R96.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R96.Value = UD61720_R96.Value - step_l;
                                    else UD61720_R96.Value = UD61720_R96.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G96.Value = UD61720_G96.Value - step_h;
                                    else UD61720_G96.Value = UD61720_G96.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G96.Value = UD61720_G96.Value - step_l;
                                    else UD61720_G96.Value = UD61720_G96.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B96.Value = UD61720_B96.Value - step_h;
                                    else UD61720_B96.Value = UD61720_B96.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B96.Value = UD61720_B96.Value - step_l;
                                    else UD61720_B96.Value = UD61720_B96.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 10:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R64.Value = UD61720_R64.Value - step_h;
                                    else UD61720_R64.Value = UD61720_R64.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R64.Value = UD61720_R64.Value - step_l;
                                    else UD61720_R64.Value = UD61720_R64.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G64.Value = UD61720_G64.Value - step_h;
                                    else UD61720_G64.Value = UD61720_G64.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G64.Value = UD61720_G64.Value - step_l;
                                    else UD61720_G64.Value = UD61720_G64.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B64.Value = UD61720_B64.Value - step_h;
                                    else UD61720_B64.Value = UD61720_B64.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B64.Value = UD61720_B64.Value - step_l;
                                    else UD61720_B64.Value = UD61720_B64.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 11:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R32.Value = UD61720_R32.Value - step_h;
                                    else UD61720_R32.Value = UD61720_R32.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R32.Value = UD61720_R32.Value - step_l;
                                    else UD61720_R32.Value = UD61720_R32.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G32.Value = UD61720_G32.Value - step_h;
                                    else UD61720_G32.Value = UD61720_G32.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G32.Value = UD61720_G32.Value - step_l;
                                    else UD61720_G32.Value = UD61720_G32.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B32.Value = UD61720_B32.Value - step_h;
                                    else UD61720_B32.Value = UD61720_B32.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B32.Value = UD61720_B32.Value - step_l;
                                    else UD61720_B32.Value = UD61720_B32.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 12:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R16.Value = UD61720_R16.Value - step_h;
                                    else UD61720_R16.Value = UD61720_R16.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R16.Value = UD61720_R16.Value - step_l;
                                    else UD61720_R16.Value = UD61720_R16.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G16.Value = UD61720_G16.Value - step_h;
                                    else UD61720_G16.Value = UD61720_G16.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G16.Value = UD61720_G16.Value - step_l;
                                    else UD61720_G16.Value = UD61720_G16.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B16.Value = UD61720_B16.Value - step_h;
                                    else UD61720_B16.Value = UD61720_B16.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B16.Value = UD61720_B16.Value - step_l;
                                    else UD61720_B16.Value = UD61720_B16.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 13:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R12.Value = UD61720_R12.Value - step_h;
                                    else UD61720_R12.Value = UD61720_R12.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R12.Value = UD61720_R12.Value - step_l;
                                    else UD61720_R12.Value = UD61720_R12.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G12.Value = UD61720_G12.Value - step_h;
                                    else UD61720_G12.Value = UD61720_G12.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G12.Value = UD61720_G12.Value - step_l;
                                    else UD61720_G12.Value = UD61720_G12.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B12.Value = UD61720_B12.Value - step_h;
                                    else UD61720_B12.Value = UD61720_B12.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B12.Value = UD61720_B12.Value - step_l;
                                    else UD61720_B12.Value = UD61720_B12.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 14:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R8.Value = UD61720_R8.Value - step_h;
                                    else UD61720_R8.Value = UD61720_R8.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R8.Value = UD61720_R8.Value - step_l;
                                    else UD61720_R8.Value = UD61720_R8.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G8.Value = UD61720_G8.Value - step_h;
                                    else UD61720_G8.Value = UD61720_G8.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G8.Value = UD61720_G8.Value - step_l;
                                    else UD61720_G8.Value = UD61720_G8.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B8.Value = UD61720_B8.Value - step_h;
                                    else UD61720_B8.Value = UD61720_B8.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B8.Value = UD61720_B8.Value - step_l;
                                    else UD61720_B8.Value = UD61720_B8.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    case 15:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R4.Value = UD61720_R4.Value - step_h;
                                    else UD61720_R4.Value = UD61720_R4.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        UD61720_R4.Value = UD61720_R4.Value - step_l;
                                    else UD61720_R4.Value = UD61720_R4.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G4.Value = UD61720_G4.Value - step_h;
                                    else UD61720_G4.Value = UD61720_G4.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        UD61720_G4.Value = UD61720_G4.Value - step_l;
                                    else UD61720_G4.Value = UD61720_G4.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B4.Value = UD61720_B4.Value - step_h;
                                    else UD61720_B4.Value = UD61720_B4.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        UD61720_B4.Value = UD61720_B4.Value - step_l;
                                    else UD61720_B4.Value = UD61720_B4.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                            UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        private int[] Parttern_UD61720 = new int[16] { 255, 252, 248, 244, 240, 224, 192, 160, 128, 96, 64, 32, 16, 12, 8, 4 };
        private void btnCalculateUD61720_Click(object sender, EventArgs e)
        {
            txtUD_255.Text = (Math.Pow((Parttern_UD61720[0] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_252.Text = (Math.Pow((Parttern_UD61720[1] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_248.Text = (Math.Pow((Parttern_UD61720[2] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_244.Text = (Math.Pow((Parttern_UD61720[3] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_240.Text = (Math.Pow((Parttern_UD61720[4] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_224.Text = (Math.Pow((Parttern_UD61720[5] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_192.Text = (Math.Pow((Parttern_UD61720[6] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_160.Text = (Math.Pow((Parttern_UD61720[7] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_128.Text = (Math.Pow((Parttern_UD61720[8] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_96.Text = (Math.Pow((Parttern_UD61720[9] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_64.Text = (Math.Pow((Parttern_UD61720[10] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_32.Text = (Math.Pow((Parttern_UD61720[11] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_16.Text = (Math.Pow((Parttern_UD61720[12] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_12.Text = (Math.Pow((Parttern_UD61720[13] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_8.Text = (Math.Pow((Parttern_UD61720[14] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");
            txtUD_4.Text = (Math.Pow((Parttern_UD61720[15] / 255.0), Convert.ToDouble(txtUD_gamma.Text)) * Convert.ToDouble(txtUD_lum.Text)).ToString("F4");

        }

        private void T20_Tick(object sender, EventArgs e)
        {
            UD61720_auto_gamma(null, null);
        }

        private void btnAutoGammas_UD61720_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[16, 3];
            D_lum = new double[16];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            D_lum[0] = Convert.ToDouble(txtUD_255.Text);
            D_lum[1] = Convert.ToDouble(txtUD_252.Text);
            D_lum[2] = Convert.ToDouble(txtUD_248.Text);
            D_lum[3] = Convert.ToDouble(txtUD_244.Text);
            D_lum[4] = Convert.ToDouble(txtUD_240.Text);
            D_lum[5] = Convert.ToDouble(txtUD_224.Text);
            D_lum[6] = Convert.ToDouble(txtUD_192.Text);
            D_lum[7] = Convert.ToDouble(txtUD_160.Text);
            D_lum[8] = Convert.ToDouble(txtUD_128.Text);
            D_lum[9] = Convert.ToDouble(txtUD_96.Text);
            D_lum[10] = Convert.ToDouble(txtUD_64.Text);
            D_lum[11] = Convert.ToDouble(txtUD_32.Text);
            D_lum[12] = Convert.ToDouble(txtUD_16.Text);
            D_lum[13] = Convert.ToDouble(txtUD_12.Text);
            D_lum[14] = Convert.ToDouble(txtUD_8.Text);
            D_lum[15] = Convert.ToDouble(txtUD_4.Text);

            D_x = Convert.ToDouble(txtUD_x.Text);
            D_y = Convert.ToDouble(txtUD_y.Text);
            Lr_d = CaSDKObj.R_y * ((D_y - CaSDKObj.G_y) * (CaSDKObj.B_x - D_x) + (D_x - CaSDKObj.G_x) * (D_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((D_y - CaSDKObj.G_y) * (D_x - CaSDKObj.R_x) + (D_x - CaSDKObj.G_x) * (CaSDKObj.R_y - D_y));
            Lb_d = 1.0;
            Lg_d = CaSDKObj.G_y * ((D_y - CaSDKObj.R_y) * (CaSDKObj.B_x - D_x) + (D_x - CaSDKObj.R_x) * (D_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((D_y - CaSDKObj.R_y) * (D_x - CaSDKObj.G_x) + (D_x - CaSDKObj.R_x) * (CaSDKObj.G_y - D_y));

            for (int i = 0; i < 16; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txtUD_loff.Text);
                        x_off = Convert.ToDouble(txtUD_xoff.Text);
                        y_off = Convert.ToDouble(txtUD_yoff.Text);
                        UD61720_GAMMA_Click(UD61720_GammaW_Red, e);
                        UD61720_GAMMA_Click(UD61720_GammaW_Green, e);
                        UD61720_GAMMA_Click(UD61720_GammaW_Blue, e);
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                        T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private void btnUDStop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }
        private void UD61720_Gamma_Load(object sender, EventArgs e)
        {

        }
    }
}
